<?php

namespace App\Http\Controllers\Api\V1;

use App\Contacts\Status\Status;
use App\Services\UserService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class UserController extends Controller
{
    private $service;

    public function __construct(Status $status, UserService $service)
    {
        parent::__construct($status);
        $this->service = $service;
    }

    /**
     * 登录验证
     * @return mixed
     */
    public function login()
    {
        if(Auth::attempt(['email' => request('email'), 'password' => request('password')]))
        {
            $user = Auth::user();
            $data =  $user->createToken('Pizza App')->accessToken;
            return $this->status->run(200,['token' => $data]);
        } else {
            return $this->status->run(401);
        }
    }

    /**
     * Create a new user instance after a valid registration.
     * @param Request $request
     * @return $this|\Illuminate\Database\Eloquent\Model
     */
    protected function create(Request $request)
    {
        $this->service->repository->create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => bcrypt($request->password),
        ]);
        return $this->status->run('200');
    }

}
